package com.shawn.admin.hander;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 元对象处理程序
 *
 * @author lenovo
 * @date 2022/10/24
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        this.setFieldValByName("created",new Date(),metaObject);

        this.setFieldValByName("updated",new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object created = this.getFieldValByName("created", metaObject);
        if (created==null){
            this.setFieldValByName("created",new Date(),metaObject);
        }
        this.setFieldValByName("updated",new Date(),metaObject);
    }
}
